﻿using System.Collections.Generic;
using System.Linq;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Entities;
using WFramework.MvcPager;
using WFramework.ORM;

namespace MiniWeb.Dao
{
    public class ShopCartDao : BaseDao<ShopCart>
    {
        public PagedList<ShopCart> GetPagedList(int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Where("1=1");
            sql.OrderBy("CreateDate DESC");
            return PagedList<ShopCart>(pageIndex, pageSize, sql);
        }

        public ShopCart GetById(int id)
        {
            var sql = Sql.Builder.Where("id = @0", id);
            return FirstOrDefault(sql);
        }

        public void Delete(int id)
        {
            var sql = Sql.Builder.Where("Id = @0", id);
            Delete(sql);
        }
        public void DeleteByAccount(string account, int id)
        {
            var sql = Sql.Builder.Where("Account = @0 AND ID = @1", account, id);
            Delete(sql);
        }
        public void DeleteCartByAccount(string account)
        {
            var sql = Sql.Builder.Where("Account = @0 ", account);
            Delete(sql);
        }
        public bool Exists(int id)
        {
            var sql = Sql.Builder.Where("Id = @0", id);
            if (FirstOrDefault(sql) != null)
            {
                return true;
            }
            return false;
        }
        public void Delete(string account,int pid)
        {
            var sql = Sql.Builder.Where("Account = @0 AND ID =@1", account, pid);
            Delete(sql);

        }

        public decimal GetTotalRMB(string account)
        {
            var sql =
                Sql.Builder.Append(
                    @"
SELECT  ISNULL(SUM(pi1.Price),0)  FROM ShopCart sc INNER JOIN ProductsInfo pi1 on sc.ProductID = pi1.ID
WHERE sc.Account =@0",
                    account);
            return FirstOrDefault<decimal>(sql);
        }

        public List<ShopCartProductInfo> GetCartList(string account)
        {
            var sql =
                Sql.Builder.Append(
                    @"SELECT sc.Id,sc.ProductID,p.Price,p.ProductName,p.ProductImg,sc.Num,(sc.Num * p.Price) AS TotalRMB 
FROM ShopCart sc INNER JOIN ProductsInfo p
ON sc.ProductID = p.ID
WHERE sc.Account =@0
",
                    account);
            return Query<ShopCartProductInfo>(sql).ToList();
        }

        public bool IsSame(string account, int pid, out int opid)
        {
            var sql =
                Sql.Builder.Append(
                    @"SELECT ID FROM ShopCart sc  WHERE sc.ProductID=@0 AND sc.Account =@1  ",
                    pid, account);
            opid = FirstOrDefault<int>(sql);
            return opid > 0;
        }
        public List<ShopCart> GetAll(int? top, string strwhere, string orderby)
        {
            string sqltop = "";
            if (top != null)
            {
                sqltop = "TOP " + top.ToString() + " * ";
            }
            else
            {
                sqltop = " * ";
            }
            var sql = Sql.Builder.Select(sqltop).From(" ShopCart WITH(NOLOCK)");
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql.Where(strwhere);
            }
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql.OrderBy(orderby);
            }
            else
            {
                sql.OrderBy("CreateDate Desc");
            }

            return Query<ShopCart>(sql).ToList();
        }



    }
}